<?php

if (!defined('MOODLE_INTERNAL')) {
    die('Direct access to this script is forbidden.');
}

global $CFG;
require_once($CFG->dirroot . '/blocks/formal_langs/scaners.php');


class block_formal_langs_userdefined_lexers_test extends UnitTestCase {

    public function echo_pre_text($title, $text) {
        echo '<pre>' . $title . '</pre>';
        echo '<pre>';
        echo $text;
        echo '</pre>';
    }
    public function echo_array($title, $array, $rowtitle='') {
        echo '<pre>' . $title . '</pre>';
        echo '<table>';
        $index = 0;
        foreach($array as $item) {
            echo '<tr><td>' . $rowtitle . $index . '</td><td>' . print_r($item) . '</td></tr>';
                $index++;
        }
        echo '</table>';
    }

    public function test_create_common_lexer() {
        global $CFG;
        require_once($CFG->dirroot . '/blocks/formal_langs/langs_code/system/lex_pattern_lexer.php');
        require_once($CFG->dirroot . '/blocks/formal_langs/langs_code/system/lex_pattern_parser.php');
        require_once($CFG->dirroot . '/blocks/formal_langs/lexical_pattern.php');
        require_once($CFG->dirroot . '/blocks/formal_langs/scaners.php');

        $lexer_rules = file_get_contents($CFG->dirroot . '/blocks/formal_langs/simpletest/test_1_lexer.txt');

        //$this->echo_pre_text('Lexer rules:', $lexer_rules);
        
        /* $lexer = new lex_pattern_lexer(fopen('data://text/plain;base64,' . base64_encode($lexer_rules), 'r')); */

        /* $this->assertNotNull($lexer, 'JLex lexer successefully created'); */
        /* $this->assertIsA($lexer, 'lex_pattern_lexer'); */
        
        /* $parser = new lex_pattern_yyParser(); */

        /* $this->assertNotNull($parser, 'phplemon parser successefully created'); */
        /* $this->assertIsA($parser, 'lex_pattern_yyParser'); */

        /* $token = null; */
        /* while ($token = $lexer->next_token()) { */
        /*     $parser->doParse($token->type(), $token->value()); */
        /* } */
        /* $parser->doParse(0); */

        /* $this->assertNotNull($parser->list); */
        /* $this->assertNotNull($parser->conditions); */

        $common_lexer = new block_formal_langs_scaner($lexer_rules);
        $this->assertNotNull($common_lexer);
        $this->assertIsA($common_lexer, 'block_formal_langs_scaner');

        $result = $common_lexer->tokenize('a b c + * ( ) q234 abc q a w bbbbb cccccc');
        $this->assertNotNull($result);

        //$this->echo_array('Common lexer rules list:', $parser->list);
        //$this->echo_array('Common lexer conditions list:', $parser->conditions);       
        //$this->assertNotNull($common_lexer, 'common lexer successefully created');
    }
    
    public function test_working() {
        global $CFG;
        $langdbrecord = new Object();
        $langdbrecord->id = 1;
        $langdbrecord->version = 1;
        $langdbrecord->parser_enabled = true;
        $langdbrecord->tech_name = "test_1";
        $langdbrecord->name = "Example language 1";
        $langdbrecord->lexer = file_get_contents($CFG->dirroot . '/blocks/formal_langs/simpletest/test_1_lexer.txt');
        $langdbrecord->parser = file_get_contents($CFG->dirroot . '/blocks/formal_langs/simpletest/test_1_parser.txt');

        $lang = new block_formal_langs_userdefined_language($langdbrecord);
        $this->assertNotNull($lang, 'Userdefined language successefully created');
        $this->assertIsA($lang, 'block_formal_langs_userdefined_language');

        $processedstring = new block_formal_langs_processed_string();
        $processedstring->string = 'a b c + * ( ) q234 abc q a w bbbbb cccccc';
        $tokens = $lang->tokenize($processedstring);
        $this->assertNotNull($tokens);
    }

    public function test_working_rus() {
        global $CFG;
        $langdbrecord = new Object();
        $langdbrecord->id = 1;
        $langdbrecord->version = 1;
        $langdbrecord->parser_enabled = true;
        $langdbrecord->tech_name = "test_1";
        $langdbrecord->name = "Example language 1";
        $langdbrecord->lexer = file_get_contents($CFG->dirroot . '/blocks/formal_langs/simpletest/test_lexer_rus.txt');
        $langdbrecord->parser = file_get_contents($CFG->dirroot . '/blocks/formal_langs/simpletest/test_parser_rus.txt');

        $lang = new block_formal_langs_userdefined_language($langdbrecord);
        $this->assertNotNull($lang, 'Userdefined language successefully created');
        $this->assertIsA($lang, 'block_formal_langs_userdefined_language');

        $processedstring = new block_formal_langs_processed_string();
        $processedstring->string = 'a b c + * ( ) q234 abc q a w bbbbb cccccc';
        $tokens = $lang->tokenize($processedstring);

        $processedstring->tokenstream->tokens = $tokens;
        $this->assertNotNull($tokens);

        //$lang->parse($processedstring, false);
    }
}
 ?>